import * as address from "../../api/address";
import validate from "../../utils/validate";

Page({
    data: {
        "id": "",
        "isDefault": "N",
        "receiveUserAddress": "",
        "cityCode": "",
        "cityName":"",
        "countryCode": "",
        "countryName": "",
        "receiveUserName": "",
        "receiveUserPhone": "",
        "provinceCode": "",
        "provinceName": "",
        "shopNo":""
    },
    onLoad(options) {

        this.setData({
            shopNo:options.shopNo
        })

        address.getShopAddressByShopNo({
            shopNo:options.shopNo
        })
            .then((res)=>{
                if(res){
                    this.setData({
                        provinceCode:res.provinceCode,
                        provinceName:res.provinceName,
                        cityCode:res.cityCode,
                        cityName:res.cityName,
                        countryCode:res.countryCode,
                        countryName:res.countryName,
                        receiveUserPhone:res.receiveUserPhone,
                        receiveUserName:res.receiveUserName,
                        receiveUserAddress:res.receiveUserAddress,
                        id:res.id
                    })
                    wx.setNavigationBarTitle({
                        title: '编辑地址'
                    });
                }
                else{
                    wx.setNavigationBarTitle({
                        title: '新增地址'
                    });
                }
            })
    },
    confirm_address_picker(e) {

        this.setData({
            provinceName: e.detail.provinceName,
            provinceCode: e.detail.provinceCode,
            cityName: e.detail.cityName,
            cityCode: e.detail.cityCode,
            countryName: e.detail.countryName,
            countryCode: e.detail.countryCode,
        })
    },
    get_input_value(e){
        let { id } = e.currentTarget;
        let value = e.detail.value;
        this.setData({
            [`${id}`]:value
        });
    },
    change_is_default(){
        if(this.data.list.length !==0 ){
            this.setData({
                isDefault:this.data.isDefault=="N"?'Y':'N'
            })
        }
    },
    submit(){
        if(!this.data.receiveUserName){
            wx.showToast({
                title: '请输入收货人姓名',
                icon:"none",
                duration: 1500,
                mask: true,
            });
            return;
        }
        if(!this.data.receiveUserPhone){
            wx.showToast({
                title: '请输入收货人电话',
                icon:"none",
                duration: 1500,
                mask: true,
            });
            return;
        }
        if( !validate.mobile.test( this.data.receiveUserPhone ) ){
            wx.showToast({
                title: '收货人电话格式错误',
                icon:"none",
                duration: 1500,
                mask: true,
            });
            return;
        }
        if(!this.data.provinceCode){
            wx.showToast({
                title: '请选择省市区县',
                icon:"none",
                duration: 1500,
                mask: true,
            });
            return;
        }
        if(!this.data.receiveUserAddress){
            wx.showToast({
                title: '请输入门牌楼层',
                icon:"none",
                duration: 1500,
                mask: true,
            });
            return;
        }

        address.add( this.data )
            .then((res)=>{
                wx.showToast({
                    title: '操作成功',
                    icon: 'none',
                    duration: 1500,
                    mask: false,
                });

                setTimeout(()=>{
                    wx.navigateBack({
                        delta: 1
                    });
                },1500);
            })
    }
})
